{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "_llm = ChatOpenAI(\n",
    "    base_url=\"http://192.168.10.11:60026/v1\",\n",
    "    model=\"qwen2.5:7b\",\n",
    "    api_key=\"ollama\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from researcher import Researcher\n",
    "from painter import Painter\n",
    "\n",
    "_researcher = Researcher(_llm)\n",
    "_painter = Painter(_llm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def _researcher_node(state):\n",
    "    return {\"messages\": [_researcher(state)]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def _painter_node(state):\n",
    "    return {\"messages\": [_painter(state)]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langgraph.graph import END\n",
    "\n",
    "\n",
    "def router(state):\n",
    "    _last_message = state[\"messages\"][-1]\n",
    "    if \"FINAL ANSWER\" in _last_message.content.upper():\n",
    "        return END\n",
    "    return \"continue\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langgraph.graph import StateGraph, MessagesState, START, END\n",
    "\n",
    "_builder = StateGraph(MessagesState)\n",
    "\n",
    "_builder.add_node(\"_researcher_node\", _researcher_node)\n",
    "_builder.add_node(\"_painter_node\", _painter_node)\n",
    "\n",
    "_builder.add_edge(START, \"_researcher_node\")\n",
    "_builder.add_conditional_edges(\n",
    "    \"_researcher_node\", router, {\"continue\": \"_painter_node\", END: END}\n",
    ")\n",
    "_builder.add_conditional_edges(\n",
    "    \"_painter_node\", router, {\"continue\": \"_researcher_node\", END: END}\n",
    ")\n",
    "\n",
    "_graph = _builder.compile()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import display, Image\n",
    "\n",
    "display(Image(_graph.get_graph().draw_mermaid_png()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for _event_dict in _graph.stream(\n",
    "    {\n",
    "        \"messages\": [\n",
    "            (\n",
    "                \"human\",\n",
    "                \"获取英国过去5年的国内生产总值。一旦你把它编码好，并执行画图，就完成。\",\n",
    "            )\n",
    "        ]\n",
    "    }\n",
    "):\n",
    "    print(_event_dict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
